<?php


namespace App\Http\Admin\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;

/*
 *  案件表接口
 *  @author 小林《1814992057@qq.com》
 *  @datetime   2019/8/23
*/

class  CaseCourtController extends Controller
{

    /*
     * 添加法院
     * @return json
     * */
    public function add_court(Request $request){
        $data=$request->only('name','status');

        if(!preg_match('/^[0-9]{1,10}$/',$data['status']))  return ThrowApiExc('70001');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');

        $res = DB::table('C_court')->insertGetId(
            [
                'name'=>$data['name'],
                'status'=>$data['status']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }


    }

    /*
     * 修改法院
     * @return json
     * */
    public function update_court(Request $request,$courtId){

//        return 123;

        $data=$request->only('name','status');
        if(!preg_match('/^[0-9]{1,10}$/',$data['status']))  return ThrowApiExc('70001');
        if(!preg_match('/^[\w\x{4e00}-\x{9fa5}]{2,20}$/u',$data['name'])) return ThrowApiExc('20031');


        $res = DB::table('C_court')->where('id',$courtId)->update(
            [
                'name'=>$data['name'],
                'status'=>$data['status']
            ]
        );

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => ['id'=>$res]
            ];
        }else{
            return ThrowApiExc('40002');
        }

    }


    /*
     * 删除法院
     * @return json
     * */

    public function delete_court($courtId){


        $res=DB::table('C_court')->where('id',$courtId)->delete();

        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => []
            ];
        }else{
            return ThrowApiExc('40003');
        }

    }


    /*
     * 查找法院
     * @return json
     * */
    public function select_court(Request $request){

        $data=$request->only('name');


        if(array_key_exists('name',$data)){
            $data=DB::table('C_court')->where('name','like','%'.$data['name'].'%')->paginate(50);

        }else{
            $data=DB::table('C_court')->paginate(50);
        }

//        $data=DB::table('C_court')->paginate(50);

        return [
            'status_code'      => 200,
            'message'     =>  '请求成功',
            'data' => $data
        ];

    }

    /*
   * 添加法院
   * @return json
   * */
    public function create(){
        return create(['court_status']);
    }


}